package com.lancoo.cvas.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.cvas.entity.*;
import com.lancoo.cvas.mapper.ClassInfoMapper;
import com.lancoo.cvas.mapper.PlanTeachingCourseSegmentMapper;
import com.lancoo.cvas.mapper.PlanTeachingStudentMapper;
import com.lancoo.cvas.mapper.SegmentMapper;
import com.lancoo.cvas.pojo.common.Deleted;
import com.lancoo.cvas.pojo.common.IsEnable;
import com.lancoo.cvas.pojo.vo.PlanTeachingToStudent;
import com.lancoo.cvas.service.ClassCourseCloseService;
import com.lancoo.cvas.service.PlanTeachingService;
import com.lancoo.cvas.service.SegmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service("segmentService")
public class SegmentServiceImpl extends ServiceImpl<SegmentMapper, Segment> implements SegmentService {


    @Resource
    private ClassInfoMapper classInfoMapper;

    @Resource
    private PlanTeachingStudentMapper planTeachingStudentMapper;

    @Resource
    private PlanTeachingCourseSegmentMapper planTeachingCourseSegmentMapper;

    @Resource
    private PlanTeachingService planTeachingService;

    @Resource
    private SegmentService segmentService;

    @Resource
    private ClassCourseCloseService classCourseCloseService;
    @Resource
    private SegmentMapper segmentMapper;


    @Override
    public List<Segment> getSegmentListByClass(Integer taskId, String classId,String baseTeachingPlanId) {


        //获取培养计划对应学生
        List<PlanTeachingToStudent> planTeachingToStudentList = planTeachingStudentMapper.selectPlanTeachingToStudentList(taskId, Deleted.NOT.getCode());

        //获取培养计划对应课程
        List<PlanTeachingCourseSegment> planTeachingCourseSegments = planTeachingCourseSegmentMapper.selectPlanTeachingCourseSegmentList(taskId, Deleted.NOT.getCode());

        //获取对应培养计划
        List<PlanTeaching> planTeachings = planTeachingService.lambdaQuery().eq(PlanTeaching::getTaskId, taskId)
                .eq(PlanTeaching::getDeleted, Deleted.NOT.getCode()).list();

        //通过课程筛选对应学生
        List<PlanTeachingToStudent> planTeachingToStudents = planTeachingToStudentList.stream()
                .filter(p -> p.getClassId().equals(classId)).collect(Collectors.toList());


        List<PlanTeaching> pTeaching = planTeachings.stream()
                .filter(p -> planTeachingToStudents.stream().anyMatch(o -> p.getBaseTeachingPlanId().equals(o.getBaseTeachingPlanId())))
                .collect(Collectors.toList());


        //获取对应环节
        List<Segment> segments = segmentMapper.getSegmentList(taskId);


        Set<String> baseSegment = new HashSet<>();


                List<PlanTeachingCourseSegment> pTeachingCourseSegments = planTeachingCourseSegments.stream()
                        .filter(p -> p.getBaseTeachingPlanId().equals(baseTeachingPlanId)
                                && p.getFlag() == 2).collect(Collectors.toList());

                if (CollUtil.isNotEmpty(pTeachingCourseSegments)) {
                    for (PlanTeachingCourseSegment pTeachingCourseSegment : pTeachingCourseSegments) {
                        if (!baseSegment.contains(pTeachingCourseSegment.getBaseCourseId())) {
                            baseSegment.add(pTeachingCourseSegment.getBaseCourseId());
                        }
                    }
                }


        //判断环节是否被禁用
        List<Segment> segmentList = new ArrayList<>();
        if (CollUtil.isNotEmpty(baseSegment)) {
            for (String c : baseSegment) {
                List<Segment> segmentCount = segments.stream()
                        .filter(p -> p.getBaseSegmentId().equals(c))
                        .collect(Collectors.toList());
                segmentList.addAll(segmentCount);
            }
        }

        //获取未启用环节，进行筛选
        List<ClassCourseClose> classCourseCloses = classCourseCloseService.lambdaQuery()
                .eq(ClassCourseClose::getTaskId, taskId)
                .eq(ClassCourseClose::getClassId, classId).eq(ClassCourseClose::getFlag, 2)
                .eq(ClassCourseClose::getBaseTeachingPlanId,baseTeachingPlanId)
                .eq(ClassCourseClose::getDeleted, Deleted.YES.getCode()).list();

        List<Long> segmentIds = classCourseCloses.stream().map(ClassCourseClose::getCourseId).distinct().collect(Collectors.toList());

        List<Segment> segmentUnEnable = segmentList.stream().filter(p -> segmentIds.stream().anyMatch(o -> p.getSegmentId().equals(o))).collect(Collectors.toList());


        segmentList.removeAll(segmentUnEnable);

        segmentUnEnable.forEach(o->{
            o.setIsEnable(IsEnable.UNABLE.getCode());
        });

        segmentList.addAll(segmentUnEnable);

        List<Segment> collect = segmentList.stream().sorted(Comparator.comparing(Segment::getSegmentId)).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<Segment> getSegmentListByMajor(Integer taskId, String gradeId, Long majorId, String baseTeachingPlanId) {

       //获取培养方案对应学生
        List<PlanTeachingToStudent> planTeachingToStudentList = planTeachingStudentMapper.selectPlanTeachingToStudentList(taskId, Deleted.NOT.getCode());

        //通过筛选获取对应行政班
        List<ClassInfo> classInfos = classInfoMapper.selectList(new LambdaQueryWrapper<ClassInfo>()
                .eq(ClassInfo::getTaskId, taskId)
                .like(ClassInfo::getMajorId, majorId)
                .eq(ClassInfo::getGradeId, gradeId));



        //获取环节
        List<Segment> segments = segmentMapper.getSegmentList(taskId);




        //获取培养方案对应课程
        List<PlanTeachingCourseSegment> planTeachingCourseSegments = planTeachingCourseSegmentMapper
                .selectPlanTeachingCourseSegmentList(taskId, Deleted.NOT.getCode());

        //获取培养方案
        List<PlanTeaching> planTeachings = planTeachingService.lambdaQuery().eq(PlanTeaching::getTaskId, taskId)
                .eq(PlanTeaching::getDeleted, Deleted.NOT.getCode()).list();


        List<PlanTeachingToStudent> planTeachingToStudents = planTeachingToStudentList.stream()
                .filter(p -> classInfos.stream().anyMatch(o -> o.getClassId().equals(p.getClassId()))).collect(Collectors.toList());

        List<PlanTeachingToStudent> planTeachingToStudents1 = planTeachingToStudents.stream().filter(p -> p.getBaseTeachingPlanId().equals(baseTeachingPlanId)).collect(Collectors.toList());

        List<String> collect1 = planTeachingToStudents1.stream().map(PlanTeachingToStudent::getClassId).distinct().collect(Collectors.toList());

        List<PlanTeaching> pTeaching = planTeachings.stream()
                .filter(p ->p.getBaseTeachingPlanId().equals(baseTeachingPlanId))
                .collect(Collectors.toList());


        //获取未启用课程
        List<ClassCourseClose> classCourseCloseList = classCourseCloseService
                .lambdaQuery().eq(ClassCourseClose::getTaskId, taskId)
                .eq(ClassCourseClose::getGradeId, gradeId)
                .eq(ClassCourseClose::getFlag, 2)
                .eq(ClassCourseClose::getBaseTeachingPlanId,baseTeachingPlanId)
                .eq(ClassCourseClose::getDeleted, Deleted.YES.getCode())
                .list();

        Set<String> baseSegment = new HashSet<>();
        if (CollUtil.isNotEmpty(pTeaching)) {
            for (PlanTeaching planTeaching : pTeaching) {

                List<PlanTeachingCourseSegment> pTeachingCourseSegments = planTeachingCourseSegments.stream()
                        .filter(p -> p.getBaseTeachingPlanId().equals(planTeaching.getBaseTeachingPlanId())
                                && p.getFlag() == 2).collect(Collectors.toList());

                if (CollUtil.isNotEmpty(pTeachingCourseSegments)) {
                    for (PlanTeachingCourseSegment pTeachingCourseSegment : pTeachingCourseSegments) {
                        if (!baseSegment.contains(pTeachingCourseSegment.getBaseCourseId())) {
                            baseSegment.add(pTeachingCourseSegment.getBaseCourseId());
                        }
                    }
                }
            }
        }


        //判断环节是否被禁用
        List<Segment> segmentList = new ArrayList<>();
        if (CollUtil.isNotEmpty(baseSegment)) {
            for (String c : baseSegment) {
                List<Segment> segmentCount = segments.stream()
                        .filter(p -> p.getBaseSegmentId().equals(c))
                        .collect(Collectors.toList());
                segmentList.addAll(segmentCount);
            }
        }



        List<PlanTeachingCourseSegment> collect2 = planTeachingCourseSegments.stream()
                .filter(p -> p.getBaseTeachingPlanId().equals(baseTeachingPlanId)).collect(Collectors.toList());

        Map<String, List<PlanTeachingCourseSegment>> collect3 = collect2.stream().filter(p -> p.getFlag().equals(2))
                .collect(Collectors.groupingBy(PlanTeachingCourseSegment::getBaseCourseId));


        Map<Long, List<ClassCourseClose>> closeMap = classCourseCloseList.stream()
                .collect(Collectors.groupingBy(ClassCourseClose::getCourseId));

        //未启用环节与应用培养方案班级数量相同则未启用，反之启用
        Integer classNum = collect1.size();
        List<Long> deletedCourseIds = new ArrayList<>();
        closeMap.forEach((o, v) -> {
            if (v.size() == classNum) {
                deletedCourseIds.add(o);
            }
        });

        List<Segment> deletedCourse = segmentList.stream().filter(p -> deletedCourseIds.stream().anyMatch(o -> o.equals(p.getSegmentId())))
                .collect(Collectors.toList());

        segmentList.removeAll(deletedCourse);

        deletedCourse.forEach(o->{
            o.setIsEnable(IsEnable.UNABLE.getCode());
        });
        segmentList.addAll(deletedCourse);

        List<Segment> collect = segmentList.stream().sorted(Comparator.comparing(Segment::getSegmentId)).collect(Collectors.toList());
        return collect;
    }
}